package com.alex.orderservice.config;

import com.alex.common.constants.CuileiQueueConstants;
import org.springframework.amqp.core.*;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 崔磊RabbitMQ配置类
 * 定义交换机、队列、绑定关系
 *
 * 就像建设邮局系统：
 * 1. 建立分拣中心（Exchange）
 * 2. 设置邮箱（Queue）
 * 3. 制定投递规则（Binding）
 */
@Configuration
public class CuileiRabbitConfig {
    /**
     * 创建崔磊订单交换机（Direct类型）
     * Direct Exchange：根据路由键精确匹配
     * 就像快递公司根据地址精确投递
     */
    @Bean
    public DirectExchange cuileiOrderExchange() {
        return ExchangeBuilder
                .directExchange(CuileiQueueConstants.CUILEI_ORDER_EXCHANGE)
                .durable(true)  // 持久化，重启不丢失
                .build();
    }

    /**
     * 创建崔磊支付队列
     * 用于处理订单支付逻辑
     */
    @Bean
    public Queue cuileiPaymentQueue() {
        return QueueBuilder
                .durable(CuileiQueueConstants.CUILEI_PAYMENT_QUEUE)
                .build();
    }


    /**
     * 创建崔磊通知队列
     * 用于处理订单通知逻辑
     */
    @Bean
    public Queue cuileiNotificationQueue() {
        return QueueBuilder
                .durable(CuileiQueueConstants.CUILEI_NOTIFICATION_QUEUE)
                .build();
    }

    /**
     * 绑定支付队列到订单交换机
     * 设置路由键：cuilei.payment
     */
    @Bean
    public Binding cuileiPaymentBinding() {
        return BindingBuilder
                .bind(cuileiPaymentQueue())
                .to(cuileiOrderExchange())
                .with(CuileiQueueConstants.CUILEI_PAYMENT_ROUTING_KEY);
    }

    /**
     * 绑定通知队列到订单交换机
     * 设置路由键：cuilei.notification
     */
    @Bean
    public Binding cuileiNotificationBinding() {
        return BindingBuilder
                .bind(cuileiNotificationQueue())
                .to(cuileiOrderExchange())
                .with(CuileiQueueConstants.CUILEI_NOTIFICATION_ROUTING_KEY);
    }
}
